###################################
### Main entry point for script ###
###################################

# parse commandline arguments
for arg in sys.argv:
	if (arg == "-h") or (arg == "--help"):
		print("")
		print("Usage: subconv.py file_in file_out [options]")
		print("")
		print("File_In:           Path of the input file")
		print("                   File extension defines the import type")
		print("                   Supported types: *.ass, *.ssa, *.srt, *.sbv")
		print("")
		print("File_Out:          Path to the output file (overwrite existing file!)")
		print("                   File extension defines the export type")
		print("                   Supported types: *.ass, *.ssa, *.srt, *.sbv")
		print("")
		print("Options:")
		print("")
		print("  -h, --help       Shows the commandline help")
		print("  -v, --version    Shows the application version")
		print("  -w, --warnings   Enable warning messages")
		print("")
		print("Examples:")
		print("")
		print("  subconv.py '/home/max/terminator.ass' '/home/max/terminator.srt'")
		print("")
		sys.exit()
	if (arg == "-v") or (arg == "--version"):
		print("")
		print("Version: " + version)
		print("")
		sys.exit()
	if (arg == "-w") or (arg == "--warning"):
		warnings = 1

# get input file from commandline arguments
if (len(sys.argv) > 1) and (sys.argv[1].strip()):
	file_in = os.path.abspath(sys.argv[1])
	file_in_dir = os.path.dirname(file_in)
	file_in_name = os.path.basename(file_in)
	file_in_ext = os.path.splitext(file_in)[1].strip().lower()
else:
	sys.exit("ERROR: no input file specified.\nUse --help for more info.")

# get output file from commandline arguments
if (len(sys.argv) > 2) and (sys.argv[2].strip()):
	file_out = os.path.abspath(sys.argv[2])
	file_out_dir = os.path.dirname(file_out)
	file_out_name = os.path.basename(file_out)
	file_out_ext = os.path.splitext(file_out)[1].strip().lower()
else:
	sys.exit("ERROR: no output file specified.\nUse -h or --help for more info.")

# check if input file exists and can be read
if (file_in):
	if not (os.path.isfile(file_in)):
		sys.exit("ERROR: input file not found!")
	if not (os.access(file_in, os.R_OK)):
		sys.exit("ERROR: input file not accessable!")
else:
	sys.exit("ERROR: invalid input file!")

# check if output path exists and filename can be created
if (file_out):
	if (os.path.isfile(file_out)):
		# output file already exist
		if not (os.access(file_out, os.W_OK)):
			sys.exit("ERROR: unable to overwrite output file!")
		else:
			if (warnings):
				print("WARNING: exisitng output file will be overwritten!")
	else:
		# output file does not exist
		if not (os.access(file_out_dir, os.W_OK)):
			sys.exit("ERROR: output directory not accessable!")
else:
	sys.exit("ERROR: invalid output file!")

# convert sbv input
if (file_in_ext == ".sbv"):
	if (file_out_ext == ".sbv"):
		sbv2sbv(file_in, file_out)
	if (file_out_ext == ".srt"):
		sbv2srt(file_in, file_out)
	if (file_out_ext == ".ssa") or (file_out_ext == ".ass"):
		sbv2ssa(file_in, file_out)

# convert srt input
if (file_in_ext == ".srt"):
	if (file_out_ext == ".srt"):
		srt2srt(file_in, file_out)
	if (file_out_ext == ".sbv"):
		srt2sbv(file_in, file_out)
	if (file_out_ext == ".ssa") or (file_out_ext == ".ass"):
		srt2ssa(file_in, file_out)

# convert ssa input
if (file_in_ext == ".ssa") or (file_in_ext == ".ass"):
	if (file_out_ext == ".ssa") or (file_out_ext == ".ass"):
		ssa2ssa(file_in, file_out)
	if (file_out_ext == ".srt"):
		ssa2srt(file_in, file_out)
	if (file_out_ext == ".sbv"):
		ssa2sbv(file_in, file_out)
